﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAPDavy.Entities;

namespace MAPDavy.Repository
{
    public class LoaderRepository:MatriculaRepository,ILoaderRepository
    {
        public int RegistrarTutores(List<MAPTemplate> maps)
        {
            var tutores = from t in maps
                          select new
                          {
                              Id = t.TeacherID,
                              LastName = t.TeacherLastName,
                              FirtsName = t.TeacherFirtsName
                          };

            var lista = tutores.Distinct().ToList();

            foreach (var item in lista)
            {
                var existe = (from t in Context.Tutors
                              where t.Id == item.Id
                              select t).SingleOrDefault();

                if (existe == null)
                {
                    Tutor newTutor = new Tutor() 
                    {
                        Id = item.Id,
                        LastName = item.LastName,
                        FirtsName = item.FirtsName,
                        Activo = true
                    };

                    Context.Tutors.Add(newTutor);                    
                }
            }

            return Context.SaveChanges();
        }

        public int AsignarTutores(List<MAPTemplate> maps)
        {
            var secTutor = (from t in maps
                           select new
                           {
                               SeccionId = t.ClassID.Value,
                               TutorId = t.TeacherID,
                               Anio = t.Year.Value.ToString() 
                           }).Distinct().ToList();

            foreach (var item in secTutor)
            {
                var existe = (from st in Context.SeccionTutors
                         where st.Anio == item.Anio && st.TutorId == item.TutorId && item.SeccionId == item.SeccionId
                         select st).SingleOrDefault();

                if (existe == null)
                {
                    SeccionTutor newSecTutor = new SeccionTutor() 
                    {
                        SeccionId = item.SeccionId,
                        TutorId = item.TutorId,
                        Anio = item.Anio
                    };
                    Context.SeccionTutors.Add(newSecTutor);                    
                }
            }

            return Context.SaveChanges();
        }

        public int RegistrarStudents(List<MAPTemplate> maps)
        {
            var students = (from s in maps
                            select new
                            {
                                Id = s.StudentID.Value.ToString(),
                                DavyId = s.DavyID.Value,
                                LastName = s.StudentLastName,
                                FirtsName = s.StudentFirtsName,
                                BirtDate = s.StudentDateBirth.Value,
                                Gender = s.StudentGender.Value.ToString(),
                                FechaIngreso = s.FechaIngreso.Value
                                
                            }).Distinct().ToList();

            foreach (var item in students)
            {
                var existe = (from s in Context.Students
                                 where s.Id == item.Id
                                 select s).SingleOrDefault();

                if (existe == null)
                {
                    Student newST = new Student()
                    {
                        Id = item.Id,
                        DavyId = item.DavyId,
                        LastName = item.LastName,
                        FirtsName = item.FirtsName,
                        BirtDate = item.BirtDate,
                        Gender = item.Gender,
                        FechaIngreso = item.FechaIngreso
                    };

                    Context.Students.Add(newST);                    
                }
                else
                {
                    existe.DavyId = item.DavyId;
                    existe.FechaIngreso = item.FechaIngreso;
                    Context.Entry(existe).State = System.Data.EntityState.Modified;
                }
            }

            return Context.SaveChanges();
        }

        public int MatricularStudents(List<MAPTemplate> maps)
        {
            var matriculas = (from m in maps
                            select new                            {
                                StudentId = m.StudentID.Value.ToString(),
                                SeccionId = m.ClassID,
                                Anio = m.Year.Value.ToString()

                            }).Distinct().ToList();

            foreach (var item in matriculas)
            {
                var existe = (from m in Context.Matriculas
                              where m.Anio == item.Anio && m.SeccionId == item.SeccionId && m.StudentId == item.StudentId
                              select m).SingleOrDefault();

                if (existe == null)
                {
                    Matricula newMat = new Matricula() 
                    {
                        StudentId = item.StudentId,
                        SeccionId = item.SeccionId,
                        Activo = true,
                        Anio = item.Anio
                    };

                    Context.Matriculas.Add(newMat);
                }
                 
            }

            return Context.SaveChanges();
       }
    }
}
